home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE6 / PD / PDF / GuiLib / GuiFlex / h / flex_c next >
Text File  |  2003-02-14  |  4KB  |  124 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //   Copyright (c) 2002, Colin Granville
  4. //
  5. //   All rights reserved.
  6. //
  7. //   Redistribution and use in source and binary forms, with or
  8. //   without modification, are permitted provided that the following 
  9. //   conditions are met:
  10. //
  11. //      * Redistributions of source code must retain the above copyright 
  12. //        notice, this list of conditions and the following disclaimer.
  13. //
  14. //      * Redistributions in binary form must reproduce the above 
  15. //        copyright notice, this list of conditions and the following 
  16. //        disclaimer in the documentation and/or other materials 
  17. //        provided with the distribution.
  18. //
  19. //      * The name Colin Granville may not be used to endorse or promote 
  20. //        products derived from this software without specific prior 
  21. //        written permission.
  22. //
  23. //   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  24. //   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  25. //   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  26. //   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
  27. //   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
  28. //   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  29. //   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  30. //   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  31. //   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  32. //   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  33. //   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
  34. //   OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //--------------------------------------------------------------------------
  37.  
  38. #ifndef GUILIB_FLEX_C_H
  39. #define GUILIB_FLEX_C_H
  40.  
  41. #include <string.h>
  42. #include "GuiFlexGlobal.h"
  43.  
  44. //
  45. // basic flex buffer - other flex buffers implimented in terms of this
  46. //
  47. class FlexChar
  48. {
  49.   public:
  50.     FlexChar(int size=0);
  51.     ~FlexChar();
  52.     char* const & anchor()                {return fanchor.data;}                      
  53.     const char* const & anchor() const    {return (const char* const &)fanchor.data;}                      
  54.     void clear();
  55.  
  56.     const char &at(int pos) const         {return fanchor.data[pos];}
  57.     char &at(int pos)                     {return fanchor.data[pos];}
  58.     const char &operator[](int pos) const {return fanchor.data[pos];}
  59.     char &operator[](int pos)             {return fanchor.data[pos];}
  60.  
  61.     int extend(int size)            {return gflex()->extend(fanchor,size);}
  62.     int resize(int size)            {return extend(size);}
  63.     int extend(int at,int by)       {return gflex()->midExtend(fanchor,at,by);}
  64.  
  65.     void move(int to, int from,int n)  {memmove(&at(to),&at(from),n);}
  66.     int size() const                   {return gflex()->size(fanchor);}
  67.  
  68.   private:
  69.     GuiFlexAnchor  fanchor;
  70.     FlexChar(const FlexChar&);    // copying not allowed - declared but not defined
  71.     void operator=(FlexChar&);    // copying not allowed - declared but not defined
  72. };
  73.  
  74. class FlexCharBuf
  75. {
  76.   public:
  77.     FlexCharBuf (int log2_block_siz=8);
  78.     char* const & anchor()                {return fc.anchor();}                      
  79.     virtual ~FlexCharBuf();
  80.     const char &at(int pos) const         {return fc.at(pos);}
  81.     char &at(int pos)                     {return fc.at(pos);}
  82.     const char &operator[](int pos) const {return at(pos);}
  83.     char &operator[](int pos)             {return at(pos);}
  84.     int size() const                      {return _size;}
  85.     int capacity() const                  {return fc.size();}
  86.  
  87.     void clear();
  88.     void move(int to, int from,int n)     {fc.move(to,from,n);}
  89.     int extend(int size);
  90.     int resize(int size);
  91.     int extend(int pos,int by);
  92.     int append(char c);
  93.     int insert(int pos,char c);
  94.  
  95.   private:
  96.     FlexChar fc;
  97.     int log2_block_size;
  98.     int _size;
  99.     int round(int size)          {return ((size>>log2_block_size)+1)<<log2_block_size;}
  100. };
  101.  
  102. inline int FlexCharBuf::extend(int size)
  103. {
  104.   return (size<=capacity() || fc.extend(round(size)))? _size=size,1:0;
  105. }
  106.  
  107. inline int FlexCharBuf::resize(int size)
  108. {
  109.   return extend(size);
  110. }
  111.  
  112. inline int FlexCharBuf::append(char c)
  113. {
  114.   return (extend(size()+1))?at(size()-1)=c,1:0;
  115. }
  116.  
  117. inline int FlexCharBuf::insert(int pos,char c)
  118. {
  119.   return (extend(pos,1))?at(pos)=c,1:0;
  120. }
  121.  
  122.  
  123. #endif
  124.